package com.example.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.ServiceExceptionUtils;
import com.example.mapper.RechargeMapper;
import com.example.po.RechargePO;
import com.example.service.AccountService;
import com.example.service.IdempotentService;
import com.example.service.RechargeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;
import javax.annotation.Resource;

/*
@description:
@ClassName RechargeServiceImpl
@author chen
@create 2025-07-09 22:24
@Version 1.0
*/
@Service
public class RechargeServiceImpl extends ServiceImpl<RechargeMapper, RechargePO> implements RechargeService
{
    @Resource
    private RechargeMapper rechargeMapper;

    @Resource
    private AccountService accountService;

    @Resource
    private TransactionTemplate transactionTemplate;


    @Resource
    private IdempotentService idempotentService;

    @Override
    public boolean rechargeCallBack1(String rechargeId)
    {
        RechargePO rechargePO = this.getById(rechargeId);
        if (rechargePO == null)
        {
            throw ServiceExceptionUtils.exception("未找到充值记录");
        }
        // 已处理成功，直接返回
        if (rechargePO.getStatus() == 1)
        {
            return true;
        }
        this.transactionTemplate.executeWithoutResult(action ->
        {
            // update updateRechargeSuccess set status = 1 where id = #{rechargeId} and status = 0
            int updateCount = this.rechargeMapper.updateRechargeSuccess(rechargeId);
            // updateCount!=1 表示未成功
            if (updateCount != 1)
            {
                throw ServiceExceptionUtils.exception("系统繁忙，请稍后重试");
            }
            //更新账户余额
            accountService.balanceAdd(rechargePO.getAccountId(), rechargePO.getPrice());
        });
        return true;
    }

    @Override
    public boolean rechargeCallBack2(String rechargeId)
    {
        RechargePO rechargePO = this.getById(rechargeId);
        if (rechargePO == null)
        {
            throw ServiceExceptionUtils.exception("未找到充值记录");
        }
        //已处理成功，直接返回
        if (rechargePO.getStatus() == 1)
        {
            return true;
        }
        // 期望版本号
        Long expectVersion = rechargePO.getVersion();
        this.transactionTemplate.executeWithoutResult(action ->
        {
            int updateCount = this.rechargeMapper.updateRechargeSuccessOptimisticLock(rechargeId, expectVersion);
            //updateCount!=1 表示未成功
            if (updateCount != 1)
            {
                throw ServiceExceptionUtils.exception("系统繁忙，请稍后重试");
            }
            //更新账户余额
            this.accountService.balanceAdd(rechargePO.getAccountId(), rechargePO.getPrice());
        });
        return true;
    }

    @Override
    public boolean rechargeCallBack3(String rechargeId)
    {
        RechargePO rechargePO = this.getById(rechargeId);

        if (rechargePO == null)
        {
            throw ServiceExceptionUtils.exception("未找到充值记录");
        }
        //已处理成功，直接返回
        if (rechargePO.getStatus() == 1)
        {
            return true;
        }
        idempotentService.idempotent(rechargeId,"RECHARGE_SUCCESS",() ->
        {
            // 将充值订单更新为成功
            rechargePO.setStatus(1);
            boolean update = this.updateById(rechargePO);
            if (!update)
            {
                throw ServiceExceptionUtils.exception("充值记录更新失败");
            }
            // 更新账户余额
            this.accountService.balanceAdd(rechargePO.getAccountId(), rechargePO.getPrice());
        });
        return true;
    }
}
